<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="业务授权" @ok="handleSubmit">
    <BasicForm @register="registerForm"/>
  </BasicModal>
</template>

<script setup lang="ts">
import {computed, ref, unref} from 'vue';
import {resAuthBusFormSchema} from './org.data';
import {useMessage} from '@/hooks/web/useMessage';
import {authBusApi} from '@/api/core/org.api';
import {BasicModal, useModalInner} from '@/components/Modal';
import {BasicForm, useForm} from '@/components/Form';

const emit = defineEmits(['success', 'register']);
const {createMessage} = useMessage();

const [registerForm, {resetFields, setFieldsValue, validate}] = useForm({
  labelWidth: 100,
  schemas: resAuthBusFormSchema,
  showActionButtonGroup: false,
});
const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
  resetFields();
  setModalProps({confirmLoading: false});
  setFieldsValue({...data.record});
});

/** 提交按钮 */
async function handleSubmit() {
  try {
    const {id, authBus} = await validate();
    setModalProps({confirmLoading: true});
    await authBusApi(id, String(authBus)).then(() => {
      closeModal();
      createMessage.success('业务授权成功！');
      emit('success');
    });
  } finally {
    setModalProps({confirmLoading: false});
  }
}
</script>
